﻿using System;
using System.Configuration;

namespace XNET.Core
{
    /// <summary>
    /// 读写配置文件
    /// </summary>
    public class XConfiguration
    {
        /// <summary>
        /// 写配置文件
        /// </summary>
        /// <param name="key">键</param>
        /// <param name="value">值</param>
        public static void SetAppSettings(string key, string value)
        {
            Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
            if (config.AppSettings.Settings[key] == null)
            {
                config.AppSettings.Settings.Add(key, value);
            }
            else
            {
                config.AppSettings.Settings[key].Value = value;
            }
            config.Save(ConfigurationSaveMode.Modified);
            ConfigurationManager.RefreshSection("appSettings");//重新加载新的配置文件   
        }

        /// <summary>
        /// 读配置文件
        /// </summary>
        /// <param name="key"></param>
        /// <returns></returns>
        public static string GeAppSettings(string key)
        {
            Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);

            if (config.AppSettings.Settings[key] == null)
                return "";
            else
                return config.AppSettings.Settings[key].Value;
        }

        /// <summary>
        /// 设置连接字符串
        /// </summary>
        /// <param name="name">连接名称</param>
        /// <param name="connectionString">字符串</param>
        /// <param name="providerName">程序提供名称</param>
        public static void SetConnectionString(string name, string connectionString, string providerName = "")
        {
            Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
            ConnectionStringSettings conn = new ConnectionStringSettings(name, connectionString, providerName);

            if (config.ConnectionStrings.ConnectionStrings[name] == null)
            {
                config.ConnectionStrings.ConnectionStrings.Add(conn);
            }
            else
            {
                config.AppSettings.Settings[name].Value = connectionString;
            }
            config.Save(ConfigurationSaveMode.Modified);
            ConfigurationManager.RefreshSection("connectionStrings");//重新加载新的配置文件   
        }
        /// <summary>
        /// 获取连接字符串
        /// </summary>
        /// <param name="name">连接名称</param>
        /// <returns></returns>
        public static string GetConnectionString(string name)
        {
            Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);

            if (config.ConnectionStrings.ConnectionStrings[name] == null)
                return "";
            else
                return config.ConnectionStrings.ConnectionStrings[name].ConnectionString;
        }
    }
}
